package com.bsj.power.pls.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.def.dto.PageDTO;
import com.bsj.power.common.def.entity.gpgga.Gpgga;
import com.bsj.power.common.def.vo.GpggaVO;
import com.bsj.power.common.mapper.GpggaMapper;
import com.bsj.power.common.util.ParamCheckUtil;
import com.bsj.power.pls.service.GpggaService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description GpggaServiceImpl
 * @time 2024/11/28 16:21
 */
@Service
public class GpggaServiceImpl implements GpggaService {

    @Resource
    private GpggaMapper gpggaMapper;

    /**
     * 获取GPGGA数据列表
     *
     * @param pageDTO
     * @param unitNo
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.GpggaVO>>
     * @author ljx
     * @time 2024/11/28 16:44
     */
    @Override
    public JsonResult<List<GpggaVO>> listGpgga(PageDTO pageDTO, Integer unitNo) {
        String startTime = pageDTO.getStartTime();
        String endTime = pageDTO.getEndTime();
        ParamCheckUtil.isAnyBlank(startTime, endTime);
        ParamCheckUtil.isNull(unitNo);
        List<GpggaVO> gpggaVOS = new ArrayList<>();
        List<Gpgga> gpggas = gpggaMapper.selectList(Wrappers.<Gpgga>lambdaQuery().eq(Gpgga::getUnitNo, unitNo)
                .between(Gpgga::getCreateTime, startTime, endTime).orderByAsc(Gpgga::getCreateTime));
        if (!CollectionUtils.isEmpty(gpggas)) {
            gpggaVOS = gpggas.stream().map(gpgga -> {
                GpggaVO gpggaVO = new GpggaVO();
                BeanUtils.copyProperties(gpgga, gpggaVO);
                double e = gpgga.getE();
                double n = gpgga.getN();
                double u = gpgga.getU();
                gpggaVO.setE(e != 0 ? e * 1000 : e);
                gpggaVO.setN(n != 0 ? n * 1000 : n);
                gpggaVO.setU(u != 0 ? u * 1000 : u);
                return gpggaVO;
            }).collect(Collectors.toList());
        }
        return JsonResult.success(gpggaVOS, gpggaVOS.size());
    }
}
